﻿package net.wangit.view.element.component;

import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.support.AbstractElementWriter;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.util.Html;

public class ResetButtonElementWriter extends AbstractElementWriter
{

	public ResetButtonElementWriter()
	{
	}

	protected void writeText(ActionContext ac, StringBuffer html, ElementComponent ec, Row row)
	{
		writeControl(ac, html, ec, row);
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html, ElementComponent ec, Row row)
	{
		writeControl(ac, html, ec, row);
	}

	protected void writeControl(ActionContext ac, StringBuffer html, ElementComponent ec, Row row)
	{
		writeControlWithDisplay(ac, html, ec, row, false);
	}

	protected void writeReadOnlyControl(ActionContext ac, StringBuffer html, ElementComponent ec, Row row)
	{
		html.append("<input type=\"reset\" value=\"").append(ElementUtil.resolveName(ac, ec)).append("\"");
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setCss(html, ec.getCss());
		Html.setStyleWithWidthAndHeight(html, ec.getWidth(), ec.getHeight());
		html.append("/>");
	}

	protected void writeDisabledControl(ActionContext ac, StringBuffer html, ElementComponent ec, Row row)
	{
		html.append("<input type=\"reset\" disabled value=\"").append(ElementUtil.resolveName(ac, ec)).append("\"");
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setCss(html, ec.getCss());
		Html.setStyleWithWidthAndHeight(html, ec.getWidth(), ec.getHeight());
		html.append("/>");
	}

	protected void writeHidden(ActionContext ac, StringBuffer html, ElementComponent ec, Row row)
	{
		writeControlWithDisplay(ac, html, ec, row, true);
	}

	private void writeControlWithDisplay(ActionContext ac, StringBuffer html, ElementComponent ec, Row row, boolean hidden)
	{
		html.append("<input type=\"reset\" value=\"").append(ElementUtil.resolveName(ac, ec)).append("\"");
		if (hidden)
			html.append(" id=\"").append(ElementUtil.getAlias(ec)).append("\" style=\"display:none\"");
		Html.setAttribute(html, ElementUtil.resolveCustom(ac, ec, row));
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setCss(html, ec.getCss());
		Html.setStyleWithWidthAndHeight(html, ec.getWidth(), ec.getHeight());
		html.append("/>");
	}
}
